package com.sc.springdata.ctrl;

import com.sc.springdata.entity.User;
import com.sc.springdata.repo.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author shi kang
 * @email shikang1008@outlook.com
 * @date 2024/6/20
 * @description Brief description of the class
 */
@RestController
@RequestMapping("users")
public class UserCtrl {
    @Autowired
    private UserRepo userRepo;

    /**
     * 通过id 直接查询到返回出去
     * @param user
     * @return
     */
    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") User user){
        System.out.println("user: " + user);
        return user;
    }


    /**
     * 通过id 直接查询到返回出去
     * @param user
     * @return
     */
    @PostMapping()
    public User saveUser(@RequestBody User user){
        System.out.println("user: " + user);
        userRepo.save(user);
        return user;
    }

    @GetMapping("/page")
    public Page getUserPage(@PageableDefault(size = 2) Pageable pageable){
        System.out.println(pageable.getPageNumber() + " " + pageable.getPageSize() + " " + pageable.getSort());
        return userRepo.findAll(pageable);
    }
}
